package com.framework.bdf4j.sysadm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.framework.bdf4j.comm.Constants;
import com.framework.bdf4j.comm.enums.DictTypeEnum;
import com.framework.bdf4j.comm.util.R;
import com.framework.bdf4j.sysadm.entity.SysDict;
import com.framework.bdf4j.sysadm.entity.SysDictItem;
import com.framework.bdf4j.sysadm.mapper.SysDictItemMapper;
import com.framework.bdf4j.sysadm.mapper.SysDictMapper;
import com.framework.bdf4j.sysadm.service.SysDictService;
import com.framework.bdf4j.sysadm.vo.Ztree;

import lombok.AllArgsConstructor;

/**
 * 字典表
 *
 */
@Service
@AllArgsConstructor
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
    private final SysDictItemMapper dictItemMapper;

    /**
     * 根据ID 删除字典
     *
     * @param id
     *            字典ID
     * @return
     */
    @Override
    @CacheEvict(value = Constants.DICT_DETAILS, allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public R removeDict(Integer id) {
        SysDict dict = this.getById(id);
        // 系统内置
        if (DictTypeEnum.SYSTEM.getType().equals(dict.getSystemFlag())) {
            return R.failed("系统内置字典不能删除");
        }

        baseMapper.deleteById(id);
        dictItemMapper.delete(Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getDictId, id));
        return R.ok();
    }

    /**
     * 更新字典
     *
     * @param dict
     *            字典
     * @return
     */
    @Override
    public R updateDict(SysDict dict) {
        SysDict sysDict = this.getById(dict.getId());
        // 系统内置
        if (DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystemFlag())) {
            return R.failed("系统内置字典不能修改");
        }
        dict.setUpdateTime(new Date());
        return R.ok(this.updateById(dict));
    }

    /**
     * 查询字典类型树
     * 
     * @param dictType
     *            字典类型
     * @return 所有字典类型
     */
    @Override
    public List<Ztree> selectDictTree(SysDict dictType) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<SysDict> dictList = this.list(Wrappers.query(dictType));
        for (SysDict dict : dictList) {
            if (Constants.STATUS_NORMAL.equals(dict.getDelFlag())) {
                Ztree ztree = new Ztree();
                ztree.setId(dict.getId());
                ztree.setName(transDictName(dict));
                ztree.setTitle(dict.getType());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    private String transDictName(SysDict dictType) {
        StringBuffer sb = new StringBuffer();
        sb.append("(" + dictType.getDescription() + ")");
        sb.append("&nbsp;&nbsp;&nbsp;" + dictType.getType());
        return sb.toString();
    }
}
